package com.baize.markwondemo;

import android.os.Build;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.MotionEvent;
import android.view.textclassifier.TextLinks;
import android.widget.TextView;

import com.baize.markwondemo.spans.CustomMentionSpan;
import com.baize.markwondemo.spans.MaskTextSpan;

/**
 * 处理菱形语法和防剧透点击冲突
 */
public class MentionMovementMethod extends LinkMovementMethod {

    private static MentionMovementMethod mInstance;

    private MentionMovementMethod() {
    }

    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer,
                                MotionEvent event) {
        int action = event.getAction();

        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            ClickableSpan[] links = buffer.getSpans(off, off, ClickableSpan.class);

            if (links.length != 0) {
                //找出优先点击的Span

                ClickableSpan link = findTargetSpan(links);
                if (action == MotionEvent.ACTION_UP) {
                    link.onClick(widget);
                } else if (action == MotionEvent.ACTION_DOWN) {
                    if (widget.getContext().getApplicationInfo().targetSdkVersion
                            >= Build.VERSION_CODES.P) {
                        // Selection change will reposition the toolbar. Hide it for a few ms for a
                        // smoother transition.
//                        widget.hideFloatingToolbar(HIDE_FLOATING_TOOLBAR_DELAY_MS);
                    }
                    Selection.setSelection(buffer,
                            buffer.getSpanStart(link),
                            buffer.getSpanEnd(link));
                }
                return true;
            } else {
                Selection.removeSelection(buffer);
            }
        }

        return super.onTouchEvent(widget, buffer, event);
    }

    private ClickableSpan findTargetSpan(ClickableSpan[] spans) {
        if (spans == null || spans.length == 0) {
            return null;
        }
        ClickableSpan mentionSpan = null;
        for (int i = 0; i < spans.length; i++) {
            //如果跨度Span中有防剧透Span，并且防剧透是遮挡状态，则交给防剧透处理
            if (spans[i] instanceof MaskTextSpan
                    && ((MaskTextSpan) spans[i]).getHideState()) {
                return spans[i];
            }
            if (spans[i] instanceof CustomMentionSpan
                && mentionSpan == null) {
                mentionSpan = spans[i];
            }
        }
        //没有防剧透Span，则看是否有MentionSpan，如果有则交给它处理
        if (mentionSpan != null) {
            return mentionSpan;
        }
        return spans[0];
    }

    public static MentionMovementMethod getInstance() {
        if (mInstance == null) {
            synchronized (MentionMovementMethod.class) {
                if (mInstance == null) {
                    mInstance = new MentionMovementMethod();
                }
            }
        }
        return mInstance;
    }

}
